package org.jeecg.modules.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.cuckoo.enums.PaymentStatusEnum;
import org.jeecg.cuckoo.enums.TransactionStatusEnum;
import org.jeecg.cuckoo.enums.TransactionTypeEnum;
import org.jeecg.modules.dto.WithdrawalInformationDto;
import org.jeecg.modules.entity.CuUserDetails;
import org.jeecg.modules.entity.CuWalletaMnagement;
import org.jeecg.modules.entity.CuWithdrawalInformation;
import org.jeecg.modules.entity.CuWithdrawalRecord;
import org.jeecg.modules.mapper.CuWithdrawalInformationMapper;
import org.jeecg.modules.mapper.CuWithdrawalRecordMapper;
import org.jeecg.modules.service.ICuUserDetailsService;
import org.jeecg.modules.service.ICuWalletaMnagementService;
import org.jeecg.modules.service.ICuWalletaTransactionService;
import org.jeecg.modules.service.ICuWithdrawalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;

/**
 * @Description: 提现记录
 * @Author: jeecg-boot
 * @Date: 2024-08-10
 * @Version: V1.0
 */
@Service
public class CuWithdrawalRecordServiceImpl extends ServiceImpl<CuWithdrawalRecordMapper, CuWithdrawalRecord> implements ICuWithdrawalRecordService {

    @Autowired
    private CuWithdrawalInformationMapper cuWithdrawalInformationMapper;

    @Autowired
    private ICuWalletaMnagementService cuWalletaMnagementService;

    @Override
    public boolean insertWithdrawalRecord(WithdrawalInformationDto informationDto, CuUserDetails userDetails) {
        // 查看提现信息是否存在
        CuWithdrawalInformation cuWithdrawalInformation = new CuWithdrawalInformation();
        cuWithdrawalInformation.setUserId(userDetails.getId());
        cuWithdrawalInformation.setType(informationDto.getType());
        QueryWrapper<CuWithdrawalInformation> queryWrapperInformation = new QueryWrapper<>(cuWithdrawalInformation);
        cuWithdrawalInformation = cuWithdrawalInformationMapper.selectOne(queryWrapperInformation);

        if (ObjectUtil.isEmpty(cuWithdrawalInformation)) {
            cuWithdrawalInformation = new CuWithdrawalInformation();
            cuWithdrawalInformation.setAccount(informationDto.getAccount());
            cuWithdrawalInformation.setName(informationDto.getName());
            cuWithdrawalInformation.setPhone(informationDto.getPhone());
            cuWithdrawalInformation.setType(informationDto.getType());
            cuWithdrawalInformation.setUserId(userDetails.getId());
            cuWithdrawalInformationMapper.insert(cuWithdrawalInformation);
        } else {
            cuWithdrawalInformation.setAccount(informationDto.getAccount());
            cuWithdrawalInformation.setName(informationDto.getName());
            cuWithdrawalInformation.setPhone(informationDto.getPhone());
            cuWithdrawalInformation.setType(informationDto.getType());
            cuWithdrawalInformation.setUserId(userDetails.getId());
            cuWithdrawalInformationMapper.updateById(cuWithdrawalInformation);
        }

        // 插入记录并冻结余额
        CuWithdrawalRecord cuWithdrawalRecord = new CuWithdrawalRecord();
        cuWithdrawalRecord.setBalance(new BigDecimal(informationDto.getBalance()));
        cuWithdrawalRecord.setUserId(userDetails.getId());
        cuWithdrawalRecord.setStart(TransactionStatusEnum.TRANSACTION_STATUS_PENDING);
        cuWithdrawalRecord.setAccount(cuWithdrawalInformation.getAccount());
        cuWithdrawalRecord.setName(cuWithdrawalInformation.getName());
        cuWithdrawalRecord.setPhone(cuWithdrawalInformation.getPhone());
        cuWithdrawalRecord.setType(cuWithdrawalInformation.getType());
        super.save(cuWithdrawalRecord);

        // 查看余额 是否充足
        BigDecimal balance = new BigDecimal(informationDto.getBalance());
        // String userId, BigDecimal amount, String type, Integer start, String description, String otherId
        boolean isUpdate = cuWalletaMnagementService.updateWalletAmount(
                userDetails.getId(),
                balance,
                TransactionTypeEnum.TRANSACTION_TYPE_OUTCOME,
                TransactionStatusEnum.TRANSACTION_STATUS_PENDING,
                "提现",
                cuWithdrawalRecord.getId()
        );
        if (!isUpdate) {
            super.removeById(cuWithdrawalRecord);
            return false;
        }
        return true;
    }
}
